package com.lzy.liujing.restaurant.controller.sysController;

import com.lzy.liujing.restaurant.entity.*;
import com.lzy.liujing.restaurant.service.DataAnalysisService;
import com.lzy.liujing.restaurant.service.GoodsCategoryService;
import com.lzy.liujing.restaurant.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 图表分析接口
 */

@RestController
@CrossOrigin
@RequestMapping("/analysis")
public class DataAnalysisController {
    @Autowired
    private DataAnalysisService dataAnalysisService;

    @Autowired
    GoodsCategoryService goodsCategoryService;
    /**
     * 查询利润数据
     *
     * @return
     */
    @PostMapping("/dailByDays/{fromDate}")
    @ResponseBody
    public Result<CountBy> getDailSalesByDays(@PathVariable String fromDate) {
        List<CountBy> result = dataAnalysisService.findgetDailSalesByDays(fromDate);

        return new Result(result, "查询成功");
    }

    @PostMapping("/gcsByDays/{fromDate}")
    @ResponseBody
    public Result<CountBy> goodsCategorySalesByDays(@PathVariable String fromDate) {
        List<CountBy> result = dataAnalysisService.findGoodsCategorySalesByDays(fromDate);
        List<GoodsDisplay> goodsDisplayList = new ArrayList<>();
        for(CountBy res : result) {
            GoodsCategory byId = goodsCategoryService.getById(res.getCategoryId());
            GoodsDisplay goodsDisplay = new GoodsDisplay(
                    byId.getCategoryName(),
                    res.getCount()
            );
            goodsDisplayList.add(goodsDisplay);
        }
        return new Result(goodsDisplayList, "查询成功");
    }
}